package com.dog.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.framework.core.exception.ServiceException;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.framework.mybatis.utils.PageUtils;
import com.dog.modules.system.convert.SysDictTypeConvert;
import com.dog.modules.system.domain.SysDictData;
import com.dog.modules.system.domain.SysDictType;
import com.dog.modules.system.api.query.SysDictTypeQuery;
import com.dog.modules.system.api.vo.SysDictTypeVO;
import com.dog.modules.system.api.vo.SysDictVO;
import com.dog.modules.system.enums.DictSourceEnum;
import com.dog.modules.system.mapper.SysDictDataMapper;
import com.dog.modules.system.mapper.SysDictTypeMapper;
import com.dog.modules.system.service.SysDictTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sys_dict_type(字典类型)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
@AllArgsConstructor
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {


    private final SysDictDataMapper sysDictDataMapper;


    @Override
    public PageResult<SysDictTypeVO> page(SysDictTypeQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<SysDictType> sysDictTypeList = this.list(getWrapper(query));
        PageInfo<SysDictType> pageInfo = new PageInfo<>(sysDictTypeList);
        List<SysDictTypeVO> sysDictTypeVOList = SysDictTypeConvert.INSTANCE.convertList(pageInfo.getList());
        return PageUtils.getPage(pageInfo, sysDictTypeVOList);
    }

    private Wrapper<SysDictType> getWrapper(SysDictTypeQuery query) {
        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getDictType()), SysDictType::getDictType, query.getDictType());
        wrapper.like(StrUtil.isNotBlank(query.getDictName()), SysDictType::getDictName, query.getDictName());
        wrapper.orderByAsc(SysDictType::getSort);

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictTypeVO vo) {
        SysDictType entity = SysDictTypeConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
    }

    @Override
    public void update(SysDictTypeVO vo) {
        SysDictType entity = SysDictTypeConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<SysDictVO.DictData> getDictSql(Long id) {
        SysDictType entity = this.getById(id);
        try {
            return sysDictDataMapper.getListForSql(entity.getDictSql());
        } catch (Exception e) {
            throw new ServiceException("动态SQL执行失败，请检查SQL是否正确！");
        }
    }

    @Override
    public List<SysDictVO> getDictList() {
        // 全部字典类型列表
        List<SysDictType> typeList = this.list(Wrappers.emptyWrapper());

        // 全部字典数据列表
        QueryWrapper<SysDictData> query = new QueryWrapper<SysDictData>().orderByAsc("sort");
        List<SysDictData> dataList = sysDictDataMapper.selectList(query);

        // 全部字典列表
        List<SysDictVO> dictList = new ArrayList<>(typeList.size());
        for (SysDictType type : typeList) {
            SysDictVO dict = new SysDictVO();
            dict.setDictType(type.getDictType());

            for (SysDictData data : dataList) {
                if (type.getId().equals(data.getDictTypeId())) {
                    dict.getDataList().add(new SysDictVO.DictData(data.getDictLabel(), data.getDictValue(),
                            data.getLabelClass()));
                }
            }

            // 数据来源动态SQL
            if (type.getDictSource() == DictSourceEnum.SQL.getValue()) {
                // 增加动态列表
                String sql = type.getDictSql();
                try {
                    dict.setDataList(sysDictDataMapper.getListForSql(sql));
                } catch (Exception e) {
                    log.error("增加动态字典异常: type=" + type, e);
                }
            }

            dictList.add(dict);
        }

        return dictList;
    }


}




